﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itfort;
using MySoft.Data;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver;
using System.Data.SqlTypes;

namespace WanPiJie.Products
{
    /// <summary>
    /// 商品销售信息服务
    /// </summary>
    public class SalesInfoService : ISalesInfoService
    {
        private DbSession dbSession;
        public SalesInfoService()
        {
            dbSession = new DbSession("Product");
        }

        /// <summary>
        /// 根据商品编号获得商品的销售信息
        /// </summary>
        /// <param name="SerialNumber">商品编号</param>
        /// <returns>特定商品编号的商品销售信息</returns>
        public List<SalesInfo> GetSalesInformation(Int64 SerialNumber)
        {
            return this.dbSession.From<_SalesInfo>().
                Where(_SalesInfo._.SerialNumber == SerialNumber).
                ToList().Convert2<SalesInfo>();
        }

        /// <summary>
        /// 更新销售信息
        /// </summary>
        /// <param name="serialNumber">商品编号</param>
        /// <param name="supplier">提供商</param>
        /// <param name="url">购买链接</param>
        /// <param name="price">价格</param>
        public void Update(long serialNumber, string supplier, string url, decimal price, string picture, int volume)
        {
            var fields = new Field[] { _SalesInfo._.Url, _SalesInfo._.Price, _SalesInfo._.Picture, _SalesInfo._.Volume, _SalesInfo._.EditeTime };
            var values = new Object[] { url, price, picture, volume, DateTime.Now };
            var condition = _SalesInfo._.SerialNumber == serialNumber
                && _SalesInfo._.Supplier == supplier;
            this.dbSession.Update<_SalesInfo>(fields, values, condition);
        }

        /// <summary>
        /// 是否存在指定的销售信息（商品）
        /// </summary>
        /// <param name="serialNumber">商品编号</param>
        /// <param name="supplier">提供商</param>
        /// <returns>True：存在,False：不存在</returns>
        public bool HasSalesInformation(long serialNumber, string supplier)
        {
            return this.dbSession.Exists<_SalesInfo>(_SalesInfo._.SerialNumber == serialNumber
                && _SalesInfo._.Supplier == supplier);
        }
    }
}
